package com.yyds.yygh.hospclient.controller.api;

import com.alibaba.csp.sentinel.util.StringUtil;
import com.mysql.jdbc.StringUtils;
import com.yyds.hospital.util.HttpRequestHelper;
import com.yyds.hospital.util.MD5;
import com.yyds.yygh.common.exception.YyghException;
import com.yyds.yygh.common.result.Result;
import com.yyds.yygh.common.result.ResultStatus;
import com.yyds.yygh.hospclient.service.DepartmentService;
import com.yyds.yygh.hospclient.service.HospitalService;
import com.yyds.yygh.hospclient.service.HospitalSetService;
import com.yyds.yygh.hospclient.service.ScheduleService;
import com.yyds.yygh.model.hosp.Department;
import com.yyds.yygh.model.hosp.Hospital;
import com.yyds.yygh.model.hosp.Schedule;
import com.yyds.yygh.vo.hosp.DepartmentQueryVo;
import com.yyds.yygh.vo.hosp.ScheduleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Api(tags = "医院Api管理")
@RestController
@RequestMapping("/api/hosp")
public class ApiController {
    @Autowired
    private HospitalService hospitalService;
    @Autowired
    private HospitalSetService hospitalSetService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private ScheduleService scheduleService;
    @ApiOperation("保存添加医院")
    @PostMapping("/saveHospital")
    public Result saveHospital(HttpServletRequest request){
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
        //获取医院签名
        String signKey = (String) map.get("sign");
        String oldSignKey = hospitalSetService.getSignKey(map.get("hoscode").toString());
        String newSignKey = MD5.encrypt(oldSignKey);
        //比较签名是否一致
        if (!signKey.equals(newSignKey)){
            throw new YyghException(ResultStatus.SIGN_ERROR);
        }

        //使用替换讲logoData字符串中的空格变为+号
        String logoData = (String) map.get("logoData");
        if (!StringUtil.isEmpty(logoData)){
            String newLogoData = logoData.replaceAll(" ", "+");
            map.put("logoData",newLogoData);
        }

        hospitalService.save(map);
        return Result.success();
    }
    @ApiOperation("查询医院方法")
    @PostMapping("/hospital/show")
    public Result findHospital(HttpServletRequest request){
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
        String hoscode = (String) map.get("hoscode");
        //判断医院是否存在
        if (StringUtils.isNullOrEmpty(hoscode)){
            throw new YyghException(ResultStatus.PARAM_ERROR);
        }
        String signKey = (String) map.get("sign");
        String oldSignKey = hospitalSetService.getSignKey(hoscode);
        String newSignKey = MD5.encrypt(oldSignKey);
        //判断签名是否正确
        if (!signKey.equals(newSignKey)){
            throw new YyghException(ResultStatus.SIGN_ERROR);
        }
        Hospital hospital = hospitalService.findHospital(hoscode);
        return Result.success(hospital);
    }
    @ApiOperation("上传科室方法")
    @PostMapping("/saveDepartment")
    public Result saveDepartment(HttpServletRequest request){

        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
        String hoscode = (String) map.get("hoscode");
        //判断医院是否存在
        if (StringUtils.isNullOrEmpty(hoscode)){
            throw new YyghException(ResultStatus.PARAM_ERROR);
        }
        String signKey = (String) map.get("sign");
        String oldSignKey = hospitalSetService.getSignKey(hoscode);
        String newSignKey = MD5.encrypt(oldSignKey);
        //判断签名是否正确
        if (!signKey.equals(newSignKey)){
            throw new YyghException(ResultStatus.SIGN_ERROR);
        }
        departmentService.save(map);

        return Result.success();
    }
    @ApiOperation("获取分页列表")
    @PostMapping("/department/list")
    public Result findDepartment(HttpServletRequest request){
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
        String hoscode = (String) map.get("hoscode");

        //校验医院code是否存在
        if (StringUtils.isNullOrEmpty(hoscode)){
            throw new YyghException(ResultStatus.PARAM_ERROR);
        }

        //校验签名是否正确
        String signKey = (String) map.get("sign");
        String oldSignKey = hospitalSetService.getSignKey(hoscode);
        String newSignKey = MD5.encrypt(oldSignKey);
        if (!signKey.equals(newSignKey)){
            throw new YyghException(ResultStatus.SIGN_ERROR);
        }

        //页码数 默认为1
        int pageNum = StringUtils.isNullOrEmpty((String) map.get("page"))? 1 :
                Integer.parseInt(map.get("page").toString());
        //每页个数 默认为10
        int pageSize = StringUtils.isNullOrEmpty((String) map.get("limit"))? 10 :
                Integer.parseInt(map.get("limit").toString());

        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);

        Page<Department> page = departmentService.findDepartment(pageNum,pageSize,departmentQueryVo);

        return Result.success(page);
    }
    @ApiOperation("删除科室信息")
    @PostMapping("/department/remove")
    public Result removeDepartment(HttpServletRequest request){
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
        String hoscode = (String) map.get("hoscode");
        //判断医院code是否存在
        if (StringUtils.isNullOrEmpty(hoscode)){
            throw new YyghException(ResultStatus.PARAM_ERROR);
        }
        String signKey = (String) map.get("sign");
        String oldSignKey = hospitalSetService.getSignKey(hoscode);
        String newSignKey = MD5.encrypt(oldSignKey);
        //核对sign是否一致
        if (!signKey.equals(newSignKey)){
            throw new YyghException(ResultStatus.SIGN_ERROR);
        }
        //判断科室是否存在
        String depcode = (String) map.get("depcode");
        if (StringUtils.isNullOrEmpty(depcode)){
            throw new YyghException(ResultStatus.PARAM_ERROR);
        }
        departmentService.removeDepartment(hoscode,depcode);
        return Result.success();
    }
    @ApiOperation("上传排班信息")
    @PostMapping("/saveSchedule")
    public Result saveSchedule(HttpServletRequest request){
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
        String hoscode = (String) map.get("hoscode");
        //判断医院编号是否存在
        if (StringUtils.isNullOrEmpty(hoscode)){
            throw new YyghException(ResultStatus.PARAM_ERROR);
        }
        //查找签名
        String signKey = (String) map.get("sign");
        String oldSignKey = hospitalSetService.getSignKey(hoscode);
        String newSignKey = MD5.encrypt(oldSignKey);
        //判断签名是否相同
        if (!signKey.equals(newSignKey)){
            throw new YyghException(ResultStatus.SIGN_ERROR);
        }
        scheduleService.save(map);
        return Result.success();
    }
    @ApiOperation("查询排班")
    @PostMapping("/schedule/list")
    public Result findSchedule(HttpServletRequest request){
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
        String hoscode = (String) map.get("hoscode");
        //判断医院编号是否存在
        if (StringUtils.isNullOrEmpty(hoscode)){
            throw new YyghException(ResultStatus.PARAM_ERROR);
        }
        //获取签名
        String signKey = (String) map.get("sign");
        String oldSignKey = hospitalSetService.getSignKey(hoscode);
        String newSignKey = MD5.encrypt(oldSignKey);
        if (!signKey.equals(newSignKey)){
            //判断签名是否一致
            throw new YyghException(ResultStatus.SIGN_ERROR);
        }
        int pageNum = StringUtils.isNullOrEmpty(map.get("page").toString())? 1 :
                Integer.parseInt(map.get("page").toString());
        int pageSize = StringUtils.isNullOrEmpty(map.get("limit").toString())? 10 :
                Integer.parseInt(map.get("limit").toString());
        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        Page<Schedule> page = scheduleService.find(pageNum,pageSize,scheduleQueryVo);
        return Result.success(page);
    }
    @ApiOperation("删除排班接口")
    @PostMapping("/schedule/remove")
    public Result removeShedule(HttpServletRequest request){
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
        String hoscode = (String) map.get("hoscode");
        //判断医院编号是否存在
        if (StringUtils.isNullOrEmpty(hoscode)){
            throw new YyghException(ResultStatus.PARAM_ERROR);
        }
        //获取签名
        String signKey = (String) map.get("sign");
        String oldSignKey = hospitalSetService.getSignKey(hoscode);
        String newSignKey = MD5.encrypt(oldSignKey);
        if (!signKey.equals(newSignKey)){
            //判断签名是否一致
            throw new YyghException(ResultStatus.SIGN_ERROR);
        }
        String hosScheduleId = (String) map.get("hosScheduleId");
        //判断排班编号是否存在
        if (StringUtils.isNullOrEmpty(hosScheduleId)){
            throw new YyghException(ResultStatus.PARAM_ERROR);
        }
        scheduleService.remove(hoscode,hosScheduleId);

        return Result.success();
    }
}
